package com.example.wallet.service.impl;

import com.example.wallet.ex.ServiceException;
import com.example.wallet.mapper.OrderMapper;
import com.example.wallet.mapper.WalletMapper;
import com.example.wallet.pojo.*;
import com.example.wallet.service.IWalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class WalletService implements IWalletService {
    @Resource
    private WalletMapper walletMapper;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 查询余额
     * @param userId  用户ID
     * @return
     */
    @Override
    public Result query(int userId) {
        Wallet query = walletMapper.query(userId);
        if(query == null){
//            return Result.fail("用户不存在");
            throw new ServiceException("用户不存在");
        }
        return Result.ok(query);
    }

    /**
     * 消费
     * @param userId 用户ID
     * @param money 消费金额
     * @return
     */
    @Override
    @Transactional
    public Result consume(int userId, double money) {
        if(money<0){
            throw new ServiceException("消费金额不能为负数");
        }
        // 查询钱包余额
        Result result = query(userId);
        if(!result.getSuccess()){
            return Result.fail(result.getErrorMsg());
        }
        Wallet wallet = (Wallet) result.getData();
        double sum = wallet.getWallet() - money;
        if(sum < 0){
//            return Result.fail("钱包余额不足");
            throw new ServiceException("钱包余额不足");
        }

        // 创建订单
        Order order = new Order();
        order.setUserId(userId);
        order.setType("consume");
        order.setMoney(money);
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        int re = orderMapper.createOrder(order);
        if(re!=1){
//            return Result.fail("创建订单失败");
            throw new ServiceException("创建订单失败");
        }

        // 扣减钱包金额
        wallet.setWallet(sum);
        int result1 = walletMapper.updataWallet(wallet);
        if(result1 != 1){
//            return Result.fail("钱包操作失败");
            throw new ServiceException("钱包操作失败");
        }
        return Result.ok(wallet);
    }

    /**
     * 退款
     * @param orderId 用户ID
     * @return
     */
    @Override
    @Transactional
    public Result refund(int orderId) {
         // 获取订单信息
        Order order = orderMapper.queryByid(orderId);
        if(order == null){
            throw new ServiceException("订单不存在");
        }
        if(!order.getType().equals("consume")){
            throw new ServiceException("无法退款");
        }
        order.setType("refund");
        order.setUpdateTime(LocalDateTime.now());

        // 修改订单信息
        int re = orderMapper.updateOrder(order);
        if(re!=1){
            throw new ServiceException("订单信息修改失败");
        }
        // 退款
        Wallet wallet = walletMapper.query(order.getUserId());
        if(wallet == null){
            throw new ServiceException("退款失败");
        }
        wallet.setWallet(wallet.getWallet()+ order.getMoney());
        int result = walletMapper.updataWallet(wallet);
        if(result != 1){
            throw new ServiceException("退款失败");
        }
        return Result.ok(wallet);
    }

    /**
     * 充值
     * @param userId 用户ID
     * @param money  充值金额
     * @return
     */
    @Override
    @Transactional
    public Result topUp(int userId, double money) {
        // 充值金额不能为负数
        if(money <0){
            throw new ServiceException("充值金额不能为负数");
        }
        // 获取钱包信息
        Wallet wallet = walletMapper.query(userId);
        if(wallet==null){
            throw new ServiceException("用户不存在");
        }
        wallet.setWallet(wallet.getWallet()+money);
        int result = walletMapper.updataWallet(wallet);
        if(result != 1){
            throw new ServiceException("钱包操作失败");
        }
        // 创建订单
        Order order = new Order();
        order.setUserId(userId);
        order.setType("topUp");
        order.setMoney(money);
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        int re = orderMapper.createOrder(order);
        if(re!=1){
//            return Result.fail("充值失败");
            throw new ServiceException("充值失败");
        }
        return Result.ok(wallet);
    }

    @Override
    public Result details(int id) {
        List<OrderDto> orders = orderMapper.queryByUserId(id);
        List<Total> totals = orderMapper.queryTotal(id);
        return Result.ok(orders,totals);
    }


}
